package main

import (
	"fmt"
	"choujiang/box_server/route"
	"choujiang/common/db"
	"choujiang/common/env"
	"choujiang/common/utils"
	"net/http"
	"os"

	"github.com/gin-gonic/gin"
	"choujiang/common/variable"
)

func main() {

	//初始化环境变量
	err := env.Init(os.Args[1])
	if err != nil {
		panic(err)
	}

	//初始化db相关
	err = db.InitDB()
	if err != nil {
		panic(err)
	}

	err = db.InitRedis()
	if err != nil {
		panic(err)
	}

	err = db.InitMgoDB()
	if err != nil {
		panic(err)
	}
	//初始化 cos
	err = utils.InitCos()
	if err != nil {
		panic(err)
	}
	//初始化 盒子参数配置
	err = variable.InitBox()
	if err != nil {
		panic(err)
	}

	if env.IsProd() {
		gin.SetMode(gin.ReleaseMode)
	} else {
		gin.SetMode(gin.DebugMode)
	}

	r := gin.New()
	router.Register(r)
	router.RegisterV2(r)

	server := http.Server{
		Addr:    ":8190",
		Handler: r,
	}

	err = server.ListenAndServe()
	if err != nil {
		fmt.Printf("-----[ListenAndServe] err: %s", err)
	}
}
